﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.ServiceLocation.Fakes;

using Happy.Command;
using Happy.Command.Test.Stub.Simple;

namespace Happy.Command.Test
{
    [TestClass]
    public class CommandServiceTest
    {
        [TestMethod]
        public void Execute_Simple_Test()
        {
            this.Fake_Simple_ServiceLocator();

            var testCommand = new TestCommand();

            CommandService.Current.Execute(testCommand);

            Assert.IsTrue(testCommand.Output.Contains(typeof(TestCommandHandler).Name));
            Assert.IsTrue(testCommand.Output.Contains(typeof(TestCommandInterceptor1Attibute).Name));
            Assert.IsTrue(testCommand.Output.Contains(typeof(TestCommandInterceptor2Attibute).Name));
            Assert.IsTrue(testCommand.Output.StartsWith("Before:" + typeof(TestCommandInterceptor1Attibute).Name));
            Assert.IsTrue(testCommand.Output.EndsWith("After:" + typeof(TestCommandInterceptor1Attibute).Name));
        }

        private void Fake_Simple_ServiceLocator()
        {
            var serviceLocator = new StubIServiceLocator();

            ServiceLocator.SetLocatorProvider(() => serviceLocator);

            serviceLocator
                .GetInstanceOf1<ICommandHandler<TestCommand>>(
                    () =>
                    {
                        return new TestCommandHandler();
                    }
                );
        }
    }
}
